package com.lsw.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.View;

import com.lsw.pullableview.Pullable;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

/**
 * Created by lsw on 2017/8/13.
 */

public class MySwipeMenuRecyclerView extends SwipeMenuRecyclerView implements Pullable {
    private boolean isCanPullup = false;
    private boolean isCanPuldown = false;
    private int[] lastPositions;

    public MySwipeMenuRecyclerView(Context context) {
        super(context);
    }

    public MySwipeMenuRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwipeMenuRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean canPullDown() {

        try {
            if (!isCanPuldown) {
                return false;
            }
            if (getAdapter().getItemCount() == 0) {
                // 没有item的时候也可以下拉刷新
                return true;
            }
            if (null != getLayoutManager()
                    && getLayoutManager() instanceof LinearLayoutManager) {
                LinearLayoutManager manager = ((LinearLayoutManager) getLayoutManager());
                return manager.findFirstCompletelyVisibleItemPosition() == 0;
            }
            if (null != getLayoutManager()
                    && getLayoutManager() instanceof GridLayoutManager) {
                GridLayoutManager manager = ((GridLayoutManager) getLayoutManager());
                return manager.findFirstCompletelyVisibleItemPosition() == 0;
            }
            if (null != getLayoutManager()
                    && getLayoutManager() instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager manager = ((StaggeredGridLayoutManager) getLayoutManager());
                return manager
                        .findFirstCompletelyVisibleItemPositions(lastPositions)[0] == 0;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    @Override
    public boolean canPullUp() {

        try {
            if (!isCanPullup) {
                return false;
            }
            if (getAdapter().getItemCount() == 0) {
                return true;
            }
            if (null != getLayoutManager()
                    && getLayoutManager() instanceof LinearLayoutManager) {
                LinearLayoutManager manager = ((LinearLayoutManager) getLayoutManager());
                int lastVisiblePosition = manager.findLastVisibleItemPosition();
                int firstVisiblePosition = manager
                        .findFirstVisibleItemPosition();
                View view = manager.getChildAt(lastVisiblePosition
                        - firstVisiblePosition);
                if (null != view) {
                    return view.getBottom() <= getMeasuredHeight()
                            && (lastVisiblePosition == getAdapter()
                            .getItemCount() - 1);
                }
            }
            if (null != getLayoutManager()
                    && getLayoutManager() instanceof GridLayoutManager) {
                GridLayoutManager manager = ((GridLayoutManager) getLayoutManager());
                int lastVisiblePosition = manager.findLastVisibleItemPosition();
                int firstVisiblePosition = manager
                        .findFirstVisibleItemPosition();
                View view = manager.getChildAt(lastVisiblePosition
                        - firstVisiblePosition);
                if (null != view) {
                    return view.getBottom() <= getMeasuredHeight()
                            && (lastVisiblePosition == getAdapter()
                            .getItemCount() - 1);
                }
            }

            if (null != getLayoutManager()
                    && getLayoutManager() instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager manager = ((StaggeredGridLayoutManager) getLayoutManager());
                if (lastPositions == null) {
                    lastPositions = new int[manager.getSpanCount()];
                }
                manager.findLastVisibleItemPositions(lastPositions);
                int lastVisibleItemPosition = findMax(lastPositions);
                int firstVisiblePosition = findMin(lastPositions);
                View view = manager.getChildAt(lastVisibleItemPosition
                        - firstVisiblePosition);
                if (null != view) {
                    return view.getBottom() <= getMeasuredHeight()
                            && (lastVisibleItemPosition == getAdapter()
                            .getItemCount() - 1);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void setCanPullup(boolean isCanPullup) {
        this.isCanPullup = isCanPullup;
    }

    public void setCanPulldown(boolean isCanPuldown) {
        this.isCanPuldown = isCanPuldown;
    }

    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }

    private int findMin(int[] lastPositions) {
        int min = lastPositions[0];
        for (int value : lastPositions) {
            if (value > min) {
                value = min;
            }
        }
        return min;
    }
}
